home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 2000 January
/
Macworld (2000-01).dmg
/
Shareware World
/
Utilities
/
Text processing
/
PDF–Blit™1.02.sea
/
PDF–Blit™1.02
/
Source
/
pdfOutput.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-09-17
|
24KB
|
970 lines
// ©1999 by Kas Thomas. All rights reserved. See ReadMe for important information.
#include <A4Stuff.h>
#include <MacTypes.h>
#include <Aliases.h>
#include <string.h>
#include <ExternalInterface.h>
#include <BBXTInterface.h>
#include "p_intern.h"
#include <stdlib.h>
#include <ctype.h>
#include "p_afmparse.h"
#include "pdfPrefs.h"
#include <math.h>
#define ExternalCallbackBlock BBXTCallbackBlock
#define ITALIC 0x0002
#define BOLDFACE 0x0001
#define NOT_ITALIC 0xFFFD
#define NOT_BOLDFACE 0xFFFE
#define SPACE 0x20
#define LINEFEED 0x0a
#define FONTSIZE 9
#define LEADING 10.0
#define TEXT_STARTX 60
#define TEXT_STARTY 750
#define LINES_PER_PAGE 60
#define CARRIAGE_RETURN 0x0d
#define TAB 0x09
#define LETTER_WIDTH letter_width
#define LETTER_HEIGHT letter_height
#define TAB_VALUE 5
#define PAGEWIDTH (8.5 * 72)
#define PAGEHEIGHT (11.5 * 72)
OSErr pdfTranslate(ExternalCallbackBlock *cb, Handle t, WindowPtr w );
void FudgeName(ExternalCallbackBlock *cb, unsigned char *str, WindowPtr w);
pascal OSErr main(ExternalCallbackBlock *callbacks,
WindowPtr w, long flags, AppleEvent *event, AppleEvent *reply);
void setDefaultPrefs( PrefsPtr p );
void JustifyLine(PDF *p, char *line, PrefsPtr pp,long targetWidth);
int CountWords(char *line);
int CountChars(char *line);
void DoHeader(PrefsPtr prefs, PDF *p, long page);
void OpenInAcrobat(ExternalCallbackBlock *cb,WindowPtr w, char *pdfFileName);
float InterpretHTML(PDF *p, char *line, PrefsPtr pp, long *i);
void SetStyle(PDF *p, PrefsPtr prefs);
char *lowerstr( char *p);
int Hex2int(char *c);
void SetTextColor(PDF *p, PrefsPtr prefs );
float LeadingValue(PDF *p,PrefsPtr prefs);
char *fontTable[] = { "","Helvetica","Helvetica-Bold","Helvetica-Oblique","Helvetica-BoldOblique",
"Times-Roman","Times-Bold","Times-Italic","Times-BoldItalic","Courier","Courier-Bold","Courier-Oblique","Courier-BoldOblique"};
char *transitionsTable[] = { "","wipe","dissolve","box","split","blinds","replace","glitter"};
unsigned char buf[1024];
// * * * * * * * * * * * * * * main() * * * * * * * * * * * * * * * *
pascal OSErr main(ExternalCallbackBlock *callbacks,
WindowPtr w, long flags, AppleEvent *event, AppleEvent *reply)
{
OSErr err = noErr;
EnterCodeResource();
{
Handle text;
WindowPtr newWindow;
if (!w) return err;
text = bbxtGetWindowContents(callbacks,w);
err = pdfTranslate(callbacks,text,w);
}
ExitCodeResource();
return err;
}
OSErr pdfTranslate( ExternalCallbackBlock *callbacks,
Handle theText,
WindowPtr w ) {
PDF *p=nil;
int font,j;
long i,textLength;
OSErr err = noErr;
Boolean timeForNewPage = false;
char okLineEnders[] = "- ;:,";
PrefsPtr prefs = nil;
unsigned char *input,filename[32];
unsigned char *out = buf;
short nuts,vRef,fref = -1;
long DirID,amt;
FSSpec fss;
long page = 1L;
float currentDepth,pointDepthLimit;
int origBaseFont;
prefs = (PrefsPtr)NewPtr(sizeof(Prefs));
err = FindFolder(kOnSystemDisk,kPreferencesFolderType,true,&vRef,&DirID);
err = FSMakeFSSpec(vRef,DirID,"\pPDF-Blit 1.0 Prefs",&fss);
err = FSpOpenDF(&fss,fsRdWrPerm,&fref);
if (err) {
err = FSpCreate(&fss,'pdfK','TEXT',smSystemScript);
if (err) return err;
setDefaultPrefs( prefs);
err = FSpOpenDF(&fss,fsRdWrPerm,&fref);
}
else {
amt = sizeof(Prefs);
err = FSRead(fref, &amt, prefs);
if (err || !amt)
setDefaultPrefs( prefs);
}
prefs->color[0] = prefs->color[1] = prefs->color[2] = 0.0;
prefs->style = 0;
if (-1 == DoDialog(callbacks, prefs)) {
if (prefs) DisposePtr((Ptr)prefs);
if (fref != -1) FSClose(fref);
return err;
}
p = PDF_new();
if (p == nil) {
return -1;
}
HLockHi(theText);
textLength = GetHandleSize(theText);
FudgeName(callbacks,filename,w);
if (PDF_open_file(p,(char *)filename )==-1){
ParamText("\pProblem opening the output file.","\p ","\p","\p");
NoteAlert(401,0);
return err;
}
PDF_set_info(p,"Creator","PDFOut 1.0");
PDF_set_info(p,"Author",(char *)prefs->author);
PDF_set_info(p,"Title",(char *)prefs->title);
PDF_set_info(p,"Keywords",(char *)prefs->keywords);
PDF_set_transition(p,(char *)transitionsTable[prefs->transition]);
PDF_begin_page(p,letter_width,letter_height);
font = PDF_findfont(p,(char *)fontTable[ prefs->font ],"default",0);
if (font ==-1){
fprintf(stderr,"Couldn't set font!\n");
HUnlock(theText);
exit(3);
}
PDF_setfont(p,font,prefs->ptsize);
PDF_set_leading(p, prefs->leading);
PDF_show(p," ");
origBaseFont = prefs->font;
pointDepthLimit = PAGEHEIGHT - prefs->marginBot;
DoHeader(prefs,p,page);
currentDepth = prefs->marginTop + LeadingValue(p,prefs);
PDF_set_text_pos(p,prefs->marginLeft,PAGEHEIGHT-currentDepth);
bbxtStartProgress(callbacks,"\p Writing PDF... Thank you for using PDF–Blit!",textLength,true);
input = *(unsigned char **)theText;
if (prefs->marginLeft + prefs->marginRt > PAGEWIDTH) goto TheEnd;
if (prefs->marginTop + prefs->marginBot > PAGEHEIGHT) goto TheEnd;
for (i = 0; i < textLength - 1 ; )
{
Boolean LineSegmented = false;
float lw = 0.,soFar = 0.;
int ourFont = PDF_get_font(p);
long desiredWidth = (long)(PAGEWIDTH - (prefs->marginLeft + prefs->marginRt));
if (prefs->justify == false)
desiredWidth -= 0.5 * prefs->marginRt;
if (bbxtDoProgress(callbacks,i))
goto TheEnd;
for (j = 0, out = buf, *(out+1)=0x00;
(lw < desiredWidth) && i < textLength - 1;
j++)
{
if (j == 0)
if (input[i] == SPACE) {
while (input[i] == SPACE)
i++;
continue;
}
PDF_set_horiz_scaling(p,(float)prefs->chwidth);
*out++ = input[i++];
if (prefs->interpHTML == true ) {
if (input[i-1] == '<' && input[i-2] == 0x5c) {
*(out - 2) = *(out - 1);
out--;
}
else if (input[i-1] == '<' && input[i-2] != 0x5c)
{
Boolean sendDown = (input[i-2] == CARRIAGE_RETURN) ? true:false;
float fsize = PDF_get_fontsize(p);
int currentFont = PDF_get_font(p);
if (input[i] == 'h' || input[i] == 'H') {
char *range = "123456";
char ch = input[i+1];
if (strchr(range,ch) != NULL) {
int fnt = PDF_findfont(p,(char *)fontTable[6],"default",0);
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
if (ch == '1') fsize *= 1.1 * 1.1 * 1.1 * 1.1 * 1.2 * 1.2;
if (ch == '2') fsize *= 1.1 * 1.1 * 1.1 * 1.1 * 1.2;
if (ch == '3') fsize *= 1.1 * 1.1 * 1.1 * 1.1;
if (ch == '4') fsize *= 1.1 * 1.1 * 1.1;
if (ch == '5') fsize *= 1.1 * 1.1;
if (ch == '6') fsize *= 1.1;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
PDF_setfont(p, fnt, fsize );
PDF_set_leading(p,fsize);
out = buf;
*out++ = input[i];
}
}
if (tolower(input[i]) == 'p' &&
tolower(input[i+1]) == 'r' &&
tolower(input[i+2]) == 'e')
{
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),
PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->font = 9;
SetStyle(p,prefs);
}
if (input[i] == 'i' || input[i] == 'I')
if (input[i+1] != 'm' && input[i+1] != 'M') {
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->style |= ITALIC;
SetStyle(p,prefs);
}
if (tolower(input[i]) == 'b' || !strncmp("strong",(char *)(input+i),6) ||
!strncmp("STRONG",(char *)(input+i),6))
{
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->style |= BOLDFACE;
SetStyle(p,prefs);
}
if (!strncmp("rect",(char *)(input+i),4) ||
!strncmp("RECT",(char *)(input+i),4) )
{
char *st,*ch = (char *) input + i + 4;
float left,bottom,width,height;
while (*ch == SPACE) ch++;
st = ch;
while (isdigit(*ch) || *ch == '.') ch++;
*ch = 0x00;
left = (float) atoi(st);
*ch = SPACE;
while (*ch == SPACE || *ch == 0x00) ch++;
st = ch;
while (isdigit(*ch) || *ch == '.') ch++;
*ch = 0x00;
bottom = (float) atoi(st);
*ch = SPACE;
while (*ch == SPACE || *ch == 0x00) ch++;
st = ch;
while (isdigit(*ch) || *ch == '.') ch++;
*ch = 0x00;
width = (float) atoi(st);
*ch = SPACE;
while (*ch == SPACE || *ch == 0x00) ch++;
st = ch;
while (isdigit(*ch) || *ch == '.') ch++;
{ char old = *ch;
*ch = 0x00;
height = (float) atoi(st);
*ch = old;
}
PDF_save(p);
PDF_rect(p,left,bottom,width,height);
PDF_fill(p);
PDF_restore(p);
}
if (!strncmp("font color=#",(char *)(input+i),12) ||
!strncmp("FONT COLOR=#",(char *)(input+i),12) )
{
float r,g,b;
int j;
char ch[3];
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
ch[0] = input[i+13]; ch[1] = input[i+14]; ch[2] = 0x00;
j = Hex2int(ch);
r = (float)j/255.;
ch[0] = input[i+15]; ch[1] = input[i+16]; ch[2] = 0x00;
j = Hex2int(ch);
g = (float)j/255.;
ch[0] = input[i+17]; ch[1] = input[i+17]; ch[2] = 0x00;
j = Hex2int(ch);
b = (float)j/255.;
prefs->color[0] = r;
prefs->color[1] = g;
prefs->color[2] = b;
SetTextColor(p,prefs);
}
if (input[i] == '!' && input[i+1] == '!')
{
out--;
i--;
while (input[i++] != '>') ;
currentDepth = pointDepthLimit;
goto NewPage;
}
if (input[i] == '#' && input[i + 1] == '3' && input[i+2] == '0')
goto TheEnd;
if (input[i] == 0x2f)
{
if (input[i+1]=='h' || input[i+1]=='H') {
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
PDF_set_leading (p, prefs->leading);
PDF_setfont(p,font,prefs->ptsize);
}
if (tolower(input[i+1])=='p' && tolower(input[i+2])=='r'
&& tolower(input[i+3])=='e')
{
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->font = origBaseFont;
SetStyle(p,prefs);
}
if (input[i+1]=='i' || input[i+1]=='I') {
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->style &= NOT_ITALIC;
SetStyle(p,prefs);
}
if (tolower(input[i+1])=='b' || !strncmp("strong",(char *)(input+i+1),6)
|| !strncmp("STRONG",(char *)(input+i+1),6)) {
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->style &= NOT_BOLDFACE;
SetStyle(p,prefs);
}
}
out--;
i--;
while (input[i++] != '>') ;
if (sendDown == true)
goto TerminateLine;
continue;
}
}
if (input[i-1] == TAB) {
int k;
for (k = 0; k < prefs->spaces; k++)
*out++ = SPACE;
}
*out = 0x00;
lw = PDF_stringwidth(p,(char *)buf,ourFont,prefs->ptsize) + soFar;
lw *= prefs->chwidth/100.;
if (input[i-1] == CARRIAGE_RETURN || LINEFEED == input[i-1]) {
if (LineSegmented == true)
{ PDF_show(p,(char *)buf); goto SegEntry; }
else goto TerminateLine;
}
}
do {
if (prefs->interpHTML == true )
{
if (input[i-1] == '<' && input[i-2] != 0x5c)
{
out--;
i--;
while (input[i++] != '>') ;
}
}
*out++ = input[i++];
if (prefs->interpHTML == true ) {
if (input[i-1] == '<' && input[i-2] == 0x5c) {
*(out - 2) = *(out - 1);
out--;
}
else if (input[i-1] == '<' && input[i-2] != 0x5c)
{
Boolean sendDown = (input[i-2] == CARRIAGE_RETURN) ? true:false;
float fsize = PDF_get_fontsize(p);
int currentFont = PDF_get_font(p);
if (input[i] == 'h' || input[i] == 'H') {
char *range = "123456";
char ch = input[i+1];
if (strchr(range,ch) != NULL) {
int fnt = PDF_findfont(p,(char *)fontTable[6],"default",0);
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
if (ch == '1') fsize *= 1.1 * 1.1 * 1.1 * 1.1 * 1.2 * 1.2;
if (ch == '2') fsize *= 1.1 * 1.1 * 1.1 * 1.1 * 1.2;
if (ch == '3') fsize *= 1.1 * 1.1 * 1.1 * 1.1;
if (ch == '4') fsize *= 1.1 * 1.1 * 1.1;
if (ch == '5') fsize *= 1.1 * 1.1;
if (ch == '6') fsize *= 1.1;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
PDF_setfont(p, fnt, fsize );
PDF_set_leading(p,fsize);
out = buf;
*out++ = input[i];
}
}
if (tolower(input[i]) == 'p' &&
tolower(input[i+1]) == 'r' &&
tolower(input[i+2]) == 'e')
{
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),
PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->font = 9;
SetStyle(p,prefs);
}
if (input[i] == 'i' || input[i] == 'I')
if (input[i+1] != 'm' && input[i+1] != 'M') {
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->style |= ITALIC;
SetStyle(p,prefs);
}
if (tolower(input[i]) == 'b' || !strncmp("strong",(char *)(input+i),6) ||
!strncmp("STRONG",(char *)(input+i),6))
{
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->style |= BOLDFACE;
SetStyle(p,prefs);
}
if (!strncmp("rect",(char *)(input+i),4) ||
!strncmp("RECT",(char *)(input+i),4) )
{
char *st,*ch = (char *) input + i + 4;
float left,bottom,width,height;
while (*ch == SPACE) ch++;
st = ch;
while (isdigit(*ch) || *ch == '.') ch++;
*ch = 0x00;
left = (float) atoi(st);
*ch = SPACE;
while (*ch == SPACE || *ch == 0x00) ch++;
st = ch;
while (isdigit(*ch) || *ch == '.') ch++;
*ch = 0x00;
bottom = (float) atoi(st);
*ch = SPACE;
while (*ch == SPACE || *ch == 0x00) ch++;
st = ch;
while (isdigit(*ch) || *ch == '.') ch++;
*ch = 0x00;
width = (float) atoi(st);
*ch = SPACE;
while (*ch == SPACE || *ch == 0x00) ch++;
{char old = *ch;
st = ch;
while (isdigit(*ch) || *ch == '.') ch++;
*ch = 0x00;
height = (float) atoi(st);
*ch = old;
PDF_save(p);
PDF_rect(p,left,bottom,width,height);
PDF_fill(p);
PDF_restore(p);
}
}
if (!strncmp("font color=#",(char *)(input+i),12) ||
!strncmp("FONT COLOR=#",(char *)(input+i),12) )
{
float r,g,b;
int j;
char ch[3];
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
ch[0] = input[i+13]; ch[1] = input[i+14]; ch[2] = 0x00;
j = Hex2int(ch);
r = (float)j/255.;
ch[0] = input[i+15]; ch[1] = input[i+16]; ch[2] = 0x00;
j = Hex2int(ch);
g = (float)j/255.;
ch[0] = input[i+17]; ch[1] = input[i+17]; ch[2] = 0x00;
j = Hex2int(ch);
b = (float)j/255.;
prefs->color[0] = r;
prefs->color[1] = g;
prefs->color[2] = b;
SetTextColor(p,prefs);
}
if (input[i] == '!' && input[i+1] == '!')
{
out--;
i--;
while (input[i++] != '>') ;
currentDepth = pointDepthLimit;
goto NewPage;
}
if (input[i] == '#' && input[i + 1] == '3' && input[i+2] == '0')
goto TheEnd;
if (input[i] == 0x2f)
{
if (input[i+1]=='h' || input[i+1]=='H') {
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
PDF_set_leading (p, prefs->leading);
PDF_setfont(p,font,prefs->ptsize);
}
if (tolower(input[i+1])=='p' && tolower(input[i+2])=='r'
&& tolower(input[i+3])=='e')
{
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->font = origBaseFont;
SetStyle(p,prefs);
}
if (input[i+1]=='i' || input[i+1]=='I') {
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->style &= NOT_ITALIC;
SetStyle(p,prefs);
}
if (tolower(input[i+1])=='b' || !strncmp("strong",(char *)(input+i+1),6)
|| !strncmp("STRONG",(char *)(input+i+1),6)) {
if (LineSegmented == false)
{
currentDepth += LeadingValue(p,prefs);
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
}
LineSegmented = true;
*(out - 1)=0x00;
soFar += PDF_stringwidth(p,(char *)buf,currentFont,fsize);
PDF_show(p,(char *)buf);
out = buf;
*out++ = input[i];
prefs->style &= NOT_BOLDFACE;
SetStyle(p,prefs);
}
}
out--;
i--;
while (input[i++] != '>') ;
if (sendDown == true)
goto TerminateLine;
continue;
}
}
} while (strchr(okLineEnders,input[i])==NULL && input[i] != CARRIAGE_RETURN
&& input[i] != LINEFEED && i < textLength - 2);
*out++ = input[i++];
*out = 0x00;
if (LineSegmented == true)
{ PDF_show(p,(char *)buf); goto SegEntry; }
if (prefs->justify == true)
JustifyLine(p,(char *)buf,prefs,desiredWidth);
else
PDF_set_horiz_scaling(p,(float)prefs->chwidth);
TerminateLine:
*out = 0x00;
currentDepth += LeadingValue(p,prefs);
SegEntry:
PDF_set_text_pos(p,(float)(prefs->marginLeft),PAGEHEIGHT-currentDepth);
if (LineSegmented == false)
PDF_show(p,(char *)buf);
NewPage:
{
int curfont = PDF_get_font(p);
float fontsize = PDF_get_fontsize(p);
float lead = LeadingValue(p,prefs);
if ( currentDepth >= pointDepthLimit ) {
PDF_end_page(p);
timeForNewPage = true;
}
if (timeForNewPage && i < textLength - 1) {
timeForNewPage = false;
PDF_begin_page(p,LETTER_WIDTH,LETTER_HEIGHT);
PDF_setfont(p,font,prefs->ptsize);
PDF_set_leading(p, prefs->leading);
DoHeader(prefs,p,++page);
SetStyle(p,prefs);
SetTextColor(p,prefs);
currentDepth = LeadingValue(p,prefs) + prefs->marginTop;
PDF_set_text_pos(p,prefs->marginLeft,PAGEHEIGHT - currentDepth);
PDF_show(p," ");
/*timeForNewPage = false;
PDF_setfont(p,curfont,fontsize);
SetStyle(p,prefs);
PDF_set_leading(p,lead);
SetTextColor(p,prefs);
currentDepth = LeadingValue(p,prefs) + prefs->marginTop;
PDF_set_text_pos(p,prefs->marginLeft,PAGEHEIGHT - currentDepth);
PDF_show(p," "); */
}
lw = 0;
}
}
TheEnd:
bbxtDoneProgress(callbacks);
PDF_end_page(p);
PDF_close(p);
HUnlock(theText);
if (fref != -1) {
amt = sizeof(Prefs);
SetEOF(fref,0L);
err = FSWrite(fref,&amt,prefs);
FSClose(fref);
}
if (prefs->launchAcrobat == true)
OpenInAcrobat(callbacks,w,(char *)filename);
if (prefs) DisposePtr((Ptr)prefs);
return err;}
char *lowerstr( char *p) {
int i;
char *ch = p;
for (i=0; *ch; i++)
ch[i] = tolower(ch[i]);
return p;
}
void FudgeName(ExternalCallbackBlock *cb, unsigned char *str, WindowPtr w) {
Str255 fName;
short v;
long d;
int length;
char ending[] = { '.','p','d','f', 0x00 };
bbxtGetDocInfo(cb,w,fName,&v,&d);
length = *fName;
BlockMove(fName+1,str,(long) *fName);
BlockMove(ending,str+length,5);
}
void OpenInAcrobat(ExternalCallbackBlock *cb,WindowPtr w, char *pdfFileName) {
Str255 fName;
short v;
long d;
FSSpec fss,appFSS;
ProcessSerialNumber psn;
extern void CopyCtoP( char *c, Str63 p);
OSErr err = noErr;
bbxtGetDocInfo(cb,w,fName,&v,&d);
CopyCtoP(pdfFileName,fName);
FSMakeFSSpec(0,0L,fName,&fss);
bbxtFindApplication(cb,'CARO', &appFSS);
err = bbxtLaunchApplication(cb,'CARO',&appFSS,&psn);
bbxtSendOpenDoc(cb,'CARO', nil, &fss,true);
}
void setDefaultPrefs( PrefsPtr p ) {
p->marginTop = p->marginBot = 72;
p->marginLeft = p->marginRt = 72;
p->font = 5;
p->transition = 6;
p->chwidth = 100;
strcpy(( char *)p->author,"Whomever");
strcpy(( char *)p->title,"Whatever");
strcpy(( char *)p->keywords,"Whichever");
p->justify = false;
p->header = false;
p->numberPages = false;
p->launchAcrobat = true;
p->interpHTML = true;
p->spaces = 5;
p->lines = 60;
p->ptsize = 10.5;
p->leading = 11.5;
p->color[0] = p->color[1] = p->color[2] = 0.0;
p->style = 0;
}
float LeadingValue(PDF *p,PrefsPtr prefs) {
float fntsize = PDF_get_fontsize(p);
if (fntsize == prefs->ptsize)
return prefs->leading;
return fntsize;
}
void SetTextColor(PDF *p, PrefsPtr prefs ) {
PDF_setrgbcolor(p,prefs->color[0],prefs->color[1],prefs->color[2]);
}
void SetStyle(PDF *p, PrefsPtr prefs)
{
int baseFamily = (prefs->font - 1)/4;
int curFont = PDF_get_font(p);
int newFont,fnt,i;
float fontsize = PDF_get_fontsize(p);
newFont = 1 + ((baseFamily * 4) | prefs->style);
fnt = PDF_findfont(p,(char *)fontTable[newFont],"default",0);
PDF_setfont(p, fnt, fontsize);
}
void JustifyLine(PDF *p, char *line, PrefsPtr pp,long targetWidth) {
float lw,rawRatio,amt_per_char;
int font = PDF_get_font(p);
float fontsize = PDF_get_fontsize(p);
char *buf = line;
int chars,words;
lw = PDF_stringwidth(p,buf,font,fontsize);
rawRatio = pp->chwidth/100.;
lw *= rawRatio;
if (lw < 0.6 * targetWidth) {
PDF_set_horiz_scaling(p, pp->chwidth);
return;
}
rawRatio = lw/targetWidth;
chars = CountChars(line);
if (!chars) return;
lw = targetWidth - lw;
lw *= 100./pp->chwidth;
PDF_set_horiz_scaling(p, pp->chwidth * (1. + .11 * lw/targetWidth) );
amt_per_char = .9 * (lw)/chars;
PDF_set_char_spacing(p, (float)((lw > 1.) ? -amt_per_char:amt_per_char));
}
int CountChars(char *line) {
char *b = line;
int i = 0;
while (*b) {
i++;
b++;
}
return i;
}
int CountWords(char *line) {
char *b = line;
int i = 0;
while (*b) {
if (*b == SPACE) {
while (*b++ == SPACE) ;
if (*b == NULL) break;
}
i++;
b++;
}
return i;
}
void DoHeader(PrefsPtr prefs, PDF *p, long page) {
float x,y,fs = PDF_get_font(p);
char tmp[2];
if (prefs->numberPages) {
x = PAGEWIDTH - prefs->marginRt;
y = PAGEHEIGHT - (.6 * prefs->marginTop);
PDF_set_text_pos(p,x,y);
sprintf((char *)tmp,"%i",(int)page);
PDF_show(p,tmp);
}
if (prefs->header == false) return;
PDF_setfont(p,fs,0.9 * prefs->ptsize);
x = prefs->marginLeft;
y = PAGEHEIGHT - (.6 * prefs->marginTop);
PDF_set_text_pos(p,x,y);
PDF_show(p,(char *)prefs->author);
y -= 0.9 * prefs->leading;
PDF_set_text_pos(p,x,y);
PDF_show(p,(char *)prefs->title);
y -= 0.9 * prefs->leading;
PDF_set_text_pos(p,x,y);
PDF_show(p,(char *)prefs->keywords);
PDF_setfont(p,fs,prefs->ptsize);
}
int Hex2int(char *c) {
char ch[2];
int value = 0;
ch[0] = *c; ch[1] = 0x00;
if (isdigit(ch[0])) value = 16 * atoi(ch);
else {
if (tolower(ch[0]) == 'a') value = 16 * 10;
if (tolower(ch[0]) == 'b') value = 16 * 11;
if (tolower(ch[0]) == 'c') value = 16 * 12;
if (tolower(ch[0]) == 'd') value = 16 * 13;
if (tolower(ch[0]) == 'e') value = 16 * 14;
if (tolower(ch[0]) == 'f') value = 16 * 15;
}
ch[0] = *(c+1);
if (isdigit(ch[0])) value += atoi(ch);
else {
if (tolower(ch[0]) == 'a') value += 10;
if (tolower(ch[0]) == 'b') value += 11;
if (tolower(ch[0]) == 'c') value += 12;
if (tolower(ch[0]) == 'd') value += 13;
if (tolower(ch[0]) == 'e') value += 14;
if (tolower(ch[0]) == 'f') value += 15;
}
return value;
}